package service

import (
	v1 "SmartLibrary/api/v1"
	"SmartLibrary/internal/model"
	"SmartLibrary/internal/repository"
	"context"
	"fmt"
	"go.uber.org/zap"
	"golang.org/x/crypto/bcrypt"
	"math"
	"time"
)

type AdminService interface {
	Login(ctx context.Context, req *v1.AdminLoginRequest) (string, error)
	GetBookLists(ctx context.Context, req *v1.AdminGetListsRequest) (*v1.AdminBookListResponse, error)
	GetBookTypes(ctx context.Context) (*[]v1.AdminBookTypeResponse, error)
	AddBooks(ctx context.Context, req *v1.AdminBook) error
	DeleteBooks(ctx context.Context, id string) error
	GetBookInformation(ctx context.Context, id string) (model.AdminBooks, error)
	PutBookInformation(ctx context.Context, v *v1.AdminPutBook) error

	GetBookTypeList(ctx context.Context, v *v1.AdminGetListsRequest) (*v1.AdminBookTypeListResponse, error)
	GetBookTypeInformation(ctx context.Context, id string) (model.AdminBookType, error)
	AddBookTypes(ctx context.Context, req *v1.AdminBookTypeRequest) error
	DeleteBookType(ctx context.Context, id string) error
	PutBookTypeInformation(ctx context.Context, req *v1.AdminBookTypeRequest) error
	GetLibraryCardsList(ctx context.Context, v *v1.AdminGetListsRequest) (*v1.AdminLibraryCardListResponse, error)
	AddLibraryCard(ctx context.Context, v *v1.AdminLibraryCardRequest) error
	GetLibraryCard(ctx context.Context, id string) (model.AdminUsers, error)
	PutLibraryCard(ctx context.Context, v *v1.AdminLibraryPutCardRequest) error
	DeleteLibraryCard(ctx context.Context, id string) error
	GetBorrowsList(ctx context.Context, v *v1.AdminGetListsRequest) (*v1.AdminBookBorrowsListResponse, error)
	GetBorrowRulesList(ctx context.Context, v *v1.AdminGetListsRequest) (*v1.AdminBookBorrowRulesListResponse, error)
	AddBorrowRules(ctx context.Context, v *v1.AdminBookBorrowRulesRequest) error
	GetBorrowRules(c context.Context, ruleId *string) (model.AdminBookRule, error)
	PutBorrowRules(ctx context.Context, v *v1.AdminPutBookBorrowRulesRequest) error
	DeleteBorrowRules(ctx context.Context, ruleId string) error
	GetBookAdminList(ctx context.Context, v *v1.AdminGetListsRequest) (*v1.AdminBookAdminsListResponse, error)
	AddBookAdmin(ctx context.Context, v *v1.AdminBookAdminRequest) error
	GetBookAdmin(ctx context.Context, adminId *string) (model.AdminBookAdmins, error)
	PutBookAdmin(ctx context.Context, v *v1.AdminPutBookAdminRequest) error
	DeleteBookAdmin(ctx context.Context, id string) error
	GetBorrowsData(ctx context.Context) (v *v1.AdminBorrowData, err error)
	GetBorrowTypeData(c context.Context) ([]v1.AdminBorrowTypeData, error)
	AddAdmin(ctx context.Context, v *v1.AdminRegisterRequest) error
	DeleteAllBooks(ctx context.Context, v *v1.AdminDeleteBooksRequest) error
	GetBorrowAllRulesList(ctx context.Context) ([]int64, error)
	PutLibraryCardRuleNumber(ctx context.Context, v *v1.AdminLibraryPutCardRuleNumberRequest) error
}

func NewAdminService(
	service *Service,
	adminRepository repository.AdminRepository,
) AdminService {
	return &adminService{
		Service:         service,
		adminRepository: adminRepository,
	}
}

type adminService struct {
	*Service
	adminRepository repository.AdminRepository
}

func (s *adminService) Login(ctx context.Context, req *v1.AdminLoginRequest) (string, error) {
	admin, err := s.adminRepository.GetByUsername(ctx, req.UserName)
	if err != nil {
		if err == v1.ErrAdminUsernameOrPasswordWrong {
			return "", v1.ErrAdminUsernameOrPasswordWrong
		}
		s.logger.Error(fmt.Sprintf("failed to get user by username when user login, username: %s", req.UserName), zap.Error(err))
		return "", v1.ErrInternalServerError
	}

	// 如果密码正确
	err = bcrypt.CompareHashAndPassword([]byte(admin.Password), []byte(req.Password))
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get user by username when verify Password, username: %s", req.UserName), zap.Error(err))
		return "", v1.ErrAdminUsernameOrPasswordWrong
	}
	// 生成 token
	token, err := s.jwt.GenToken(admin.AdminID, time.Now().Add(time.Hour*24*90))
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get user by username when generate token , username: %s", req.UserName), zap.Error(err))
		return "", v1.ErrInternalServerError
	}

	return token, nil
}

func (s *adminService) GetBookLists(ctx context.Context, req *v1.AdminGetListsRequest) (*v1.AdminBookListResponse, error) {
	var adminBookResponse v1.AdminBookListResponse
	bookLists, err := s.adminRepository.GetBookLists(ctx, req)
	if err != nil {
		s.logger.Info(fmt.Sprintf("failed to get book list when admin get book list, err: %v", err))
		return nil, err
	}
	booksCount, err := s.adminRepository.GetBooksCount(ctx, req)
	if err != nil {
		s.logger.Info(fmt.Sprintf("failed to get book list when admin get book list count, err: %v", err))
		return nil, err
	}
	adminBookResponse.Records = *bookLists
	adminBookResponse.Total = int(booksCount)
	adminBookResponse.Pages = int(math.Ceil(float64(adminBookResponse.Total) / float64(req.Limit)))

	return &adminBookResponse, err
}
func (s *adminService) GetBookTypes(ctx context.Context) (*[]v1.AdminBookTypeResponse, error) {
	bookTypes, err := s.adminRepository.GetBookTypes(ctx)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get bookType list when admin get bookType list, err: %v", err))
		return &[]v1.AdminBookTypeResponse{}, err
	}
	return bookTypes, nil
}

func (s *adminService) AddBooks(ctx context.Context, req *v1.AdminBook) error {
	err := s.adminRepository.AddBooks(ctx, req)
	return err
}

func (s *adminService) DeleteBooks(ctx context.Context, id string) error {
	err := s.adminRepository.DeleteBooks(ctx, id)
	if err != nil {
		s.logger.Info(fmt.Sprintf("failed to get bookId when admin deleteBook, err: %v", err))
		return err
	}
	return nil
}
func (s *adminService) GetBookInformation(ctx context.Context, id string) (model.AdminBooks, error) {
	bookInformation, err := s.adminRepository.GetBookInformation(ctx, id)
	if err != nil {
		if err == v1.ErrAdminBookNotFound {
			s.logger.Info(fmt.Sprintf("failed to get bookId list when admin getBookInformation, err: %v", err))
			return model.AdminBooks{}, v1.ErrBadRequest
		}
		s.logger.Error(fmt.Sprintf("failed to get bookId list when admin getBookInformation, err: %v", err))
		return model.AdminBooks{}, v1.ErrInternalServerError
	}
	return bookInformation, err
}
func (s *adminService) PutBookInformation(ctx context.Context, v *v1.AdminPutBook) error {
	err := s.adminRepository.PutBookInformation(ctx, v)
	if err != nil {
		if err == v1.ErrAdminBookNotFound {
			s.logger.Info(fmt.Sprintf("failed to get bookId list when admin putBookInformation, err: %v", err))
			return v1.ErrBadRequest
		}
		s.logger.Error(fmt.Sprintf("failed to get bookInformation list when admin putBookInformation list, err: %v", err))
		return err
	}
	return nil
}
func (s *adminService) GetBookTypeList(ctx context.Context, req *v1.AdminGetListsRequest) (*v1.AdminBookTypeListResponse, error) {
	var BookTypeListResponse v1.AdminBookTypeListResponse
	bookTypes, err := s.adminRepository.GetBookTypeList(ctx, req)
	if err != nil {
		s.logger.Info(fmt.Sprintf("bookType list is nil when admin get bookType list, err: %v", err))
		return nil, err
	}
	bookCount, err := s.adminRepository.GetBookTypeCount(ctx, req)
	if err != nil {
		s.logger.Info(fmt.Sprintf("bookType list is nil when admin get bookType list, err: %v", err))
		return nil, err
	}
	BookTypeListResponse.Records = bookTypes
	BookTypeListResponse.Total = int(bookCount)
	BookTypeListResponse.Pages = int(math.Ceil(float64(BookTypeListResponse.Total) / float64(req.Limit)))
	return &BookTypeListResponse, err
}
func (s *adminService) GetBookTypeInformation(ctx context.Context, id string) (model.AdminBookType, error) {
	BookTypeInformation, err := s.adminRepository.GetBookTypeInformation(ctx, id)
	if err != nil {
		if err == v1.ErrAdminBookTypeNotFound {
			return model.AdminBookType{}, v1.ErrAdminBookTypeNotFound
		}
		s.logger.Error(fmt.Sprintf("bookTypeinformation is nil when admin get bookTypeinformation, err: %v", err))
	}
	return BookTypeInformation, err
}
func (s *adminService) AddBookTypes(ctx context.Context, req *v1.AdminBookTypeRequest) error {
	err := s.adminRepository.AddBookTypes(ctx, req)
	if err != nil {
		if err == v1.ErrAdminBookTypeDuplicate {
			s.logger.Info(fmt.Sprintf("failed to add bookType when admin add bookType, err: %v", err))
			return v1.ErrAdminBookTypeDuplicate
		}
		s.logger.Error(fmt.Sprintf("failed to add bookType when admin add bookType, err: %v", err))
		return err
	}
	return nil
}
func (s *adminService) DeleteBookType(ctx context.Context, id string) error {
	err := s.adminRepository.DeleteBookType(ctx, id)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get bookType when admin get bookType, err: %v", err))
	}
	return err
}
func (s *adminService) PutBookTypeInformation(ctx context.Context, req *v1.AdminBookTypeRequest) error {
	err := s.adminRepository.PutBookTypeInformation(ctx, req)
	if err != nil {
		if err == v1.ErrAdminBookTypeNotFound {
			return v1.ErrAdminBookTypeNotFound
		}
	}
	return err
}
func (s *adminService) GetLibraryCardsList(ctx context.Context, v *v1.AdminGetListsRequest) (*v1.AdminLibraryCardListResponse, error) {
	var adminLibraryCardResponse v1.AdminLibraryCardListResponse
	libraryCards, err := s.adminRepository.GetLibraryCardsList(ctx, v)
	if err != nil {
		s.logger.Info(fmt.Sprintf("failed to get libraryCards list when admin get libraryCards list, err: %v", err))
		return nil, err
	}
	libraryCardsCount, err := s.adminRepository.GetLibraryCardsCount(ctx, v)
	if err != nil {
		s.logger.Info(fmt.Sprintf("failed to get libraryCardsCount list when admin get libraryCardsCount list, err: %v", err))
		return nil, err
	}
	adminLibraryCardResponse.Records = libraryCards
	adminLibraryCardResponse.Total = int(libraryCardsCount)
	adminLibraryCardResponse.Pages = int(math.Ceil(float64(adminLibraryCardResponse.Total) / float64(v.Limit)))
	return &adminLibraryCardResponse, err
}
func (s *adminService) AddLibraryCard(ctx context.Context, v *v1.AdminLibraryCardRequest) error {
	err := s.adminRepository.AddLibraryCard(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to add libraryCard when admin add libraryCard, err: %v", err))
		return err
	}
	return nil
}
func (s *adminService) GetLibraryCard(ctx context.Context, id string) (model.AdminUsers, error) {
	card, err := s.adminRepository.GetLibraryCard(ctx, id)
	if err != nil {
		if err == v1.ErrAdminLibraryCardNotFound {
			s.logger.Info(fmt.Sprintf("failed to get libraryCard when admin get libraryCardInformation, err: %v", err))
			return model.AdminUsers{}, v1.ErrAdminLibraryCardNotFound
		}
		s.logger.Error(fmt.Sprintf("failed to get libraryCard when admin get libraryCardInformation, err: %v", err))
	}
	return card, err
}
func (s *adminService) PutLibraryCard(ctx context.Context, v *v1.AdminLibraryPutCardRequest) error {
	err := s.adminRepository.PutLibraryCard(ctx, v)
	if err != nil {
		if err == v1.ErrAdminUsernameAlreadyUse {
			s.logger.Info(fmt.Sprintf("failed to put libraryCard when admin put libraryCardInformation, err: %v", err))
			return v1.ErrAdminUsernameAlreadyUse
		}
		if err == v1.ErrAdminLibraryCardNotFound {
			s.logger.Info(fmt.Sprintf("failed to put libraryCard when admin put libraryCardInformation, err: %v", err))
			return v1.ErrBadRequest
		}
		s.logger.Error(fmt.Sprintf("failed to put libraryCard when admin put libraryCardInformation, err: %v", err))
	}
	return err
}
func (s *adminService) DeleteLibraryCard(ctx context.Context, id string) error {
	err := s.adminRepository.DeleteLibraryCard(ctx, id)
	if err != nil {
		if err == v1.ErrAdminLibraryCardNotFound {
			s.logger.Info(fmt.Sprintf("failed to delete libraryCard when admin delete libraryCard, err: %v", err))
			return v1.ErrBadRequest
		}
		s.logger.Error(fmt.Sprintf("failed to delete libraryCard when admin delete libraryCard, err: %v", err))
		return err
	}
	return nil
}
func (s *adminService) GetBorrowsList(ctx context.Context, v *v1.AdminGetListsRequest) (*v1.AdminBookBorrowsListResponse, error) {
	var adminBookListResponse v1.AdminBookBorrowsListResponse
	borrows, err := s.adminRepository.GetBorrowsList(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get borrows list when admin get borrows list, err: %v", err))
	}
	borrowsCount, err := s.adminRepository.GetBorrowsCount(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get borrows list when admin get borrows list, err: %v", err))
	}
	adminBookListResponse.Records = borrows
	adminBookListResponse.Total = int(borrowsCount)
	adminBookListResponse.Pages = int(math.Ceil(float64(adminBookListResponse.Total) / float64(v.Limit)))
	return &adminBookListResponse, err
}
func (s *adminService) GetBorrowAllRulesList(ctx context.Context) ([]int64, error) {
	return s.adminRepository.GetBorrowAllRulesList(ctx)
}
func (s *adminService) GetBorrowRulesList(ctx context.Context, v *v1.AdminGetListsRequest) (*v1.AdminBookBorrowRulesListResponse, error) {
	var adminBookListResponse v1.AdminBookBorrowRulesListResponse
	borrowRules, err := s.adminRepository.GetBorrowRulesList(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get borrowRules list when admin get borrowRules list, err: %v", err))
	}
	borrowRulesCount, err := s.adminRepository.GetBorrowRulesCount(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get borrowRules list when admin get borrowRules list, err: %v", err))
	}
	adminBookListResponse.Records = borrowRules
	adminBookListResponse.Total = int(borrowRulesCount)
	adminBookListResponse.Pages = int(math.Ceil(float64(adminBookListResponse.Total) / float64(v.Limit)))
	return &adminBookListResponse, err
}
func (s *adminService) AddBorrowRules(ctx context.Context, v *v1.AdminBookBorrowRulesRequest) error {
	err := s.adminRepository.AddBorrowRules(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to add borrowRules when admin add borrowRules, err: %v", err))
	}
	return err
}
func (s *adminService) GetBorrowRules(c context.Context, ruleId *string) (model.AdminBookRule, error) {
	rules, err := s.adminRepository.GetBorrowRules(c, ruleId)
	if err != nil {
		if err == v1.ErrAdminBookBorrowRulesNotFound {
			return model.AdminBookRule{}, v1.ErrAdminBookBorrowRulesNotFound
		}
		s.logger.Error(fmt.Sprintf("failed to get borrowRules when admin get borrowRules, err: %v", err))
	}
	return rules, err
}
func (s *adminService) PutBorrowRules(ctx context.Context, v *v1.AdminPutBookBorrowRulesRequest) error {
	err := s.adminRepository.PutBorrowRules(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to put borrowRules when admin put borrowRules, err: %v", err))
	}
	return err
}
func (s *adminService) DeleteBorrowRules(ctx context.Context, ruleId string) error {
	err := s.adminRepository.DeleteBorrowRules(ctx, ruleId)
	if err != nil {
		if err == v1.ErrAdminBookBorrowRulesNotFound {
			return v1.ErrAdminBookBorrowRulesNotFound
		}
		s.logger.Error(fmt.Sprintf("failed to delete borrowRules when admin delete borrowRules, err: %v", err))
	}
	return err
}
func (s *adminService) GetBookAdminList(ctx context.Context, v *v1.AdminGetListsRequest) (*v1.AdminBookAdminsListResponse, error) {
	var adminBookListResponse v1.AdminBookAdminsListResponse
	admins, err := s.adminRepository.GetBookAdminList(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get bookAdmins list when admin get bookAdmins list, err: %v", err))
	}
	bookAdminsCount, err := s.adminRepository.GetBookAdminsCount(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get bookAdmins list when admin get bookAdmins list, err: %v", err))
	}
	adminBookListResponse.Records = admins
	adminBookListResponse.Total = int(bookAdminsCount)
	adminBookListResponse.Pages = int(math.Ceil(float64(adminBookListResponse.Total) / float64(v.Limit)))
	return &adminBookListResponse, err
}
func (s *adminService) AddBookAdmin(ctx context.Context, v *v1.AdminBookAdminRequest) error {
	err := s.adminRepository.AddBookAdmin(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to add bookAdmin when admin add bookAdmin, err: %v", err))
		if err == v1.ErrAdminBookAdminsDuplicate {
			return v1.ErrAdminBookAdminsDuplicate
		}
	}
	return err
}
func (s *adminService) GetBookAdmin(ctx context.Context, adminId *string) (model.AdminBookAdmins, error) {
	admin, err := s.adminRepository.GetBookAdmin(ctx, adminId)
	if err != nil {
		if err == v1.ErrAdminBookAdminsNotFound {
			s.logger.Info(fmt.Sprintf("failed to get bookAdmin when admin get bookAdmin, err: %v", err))
			return model.AdminBookAdmins{}, v1.ErrAdminBookAdminsNotFound
		}
		s.logger.Error(fmt.Sprintf("failed to get bookAdmin when admin get bookAdmin, err: %v", err))
	}
	return admin, err
}
func (s *adminService) PutBookAdmin(ctx context.Context, v *v1.AdminPutBookAdminRequest) error {
	err := s.adminRepository.PutBookAdmin(ctx, v)
	if err != nil {
		if err == v1.ErrAdminBookAdminsNotFound {
			return v1.ErrAdminBookAdminsNotFound
		}
		s.logger.Error(fmt.Sprintf("failed to put bookAdmin when admin put bookAdmin, err: %v", err))
	}
	return err
}
func (s *adminService) DeleteBookAdmin(ctx context.Context, id string) error {
	err := s.adminRepository.DeleteBookAdmin(ctx, id)
	if err != nil {
		if err == v1.ErrAdminBookAdminsNotFound {
			s.logger.Info(fmt.Sprintf("failed to delete bookAdmin when admin delete bookAdmin, err: %v", err))
			return v1.ErrBadRequest
		}
		s.logger.Error(fmt.Sprintf("failed to delete bookAdmin when admin delete bookAdmin, err: %v", err))
	}
	return err
}

func (s *adminService) GetBorrowsData(ctx context.Context) (v *v1.AdminBorrowData, err error) {
	borrowsRecord, err := s.adminRepository.GetBorrowsData(ctx)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get borrows data when admin get borrows data, err: %v", err))
	}
	borrowsDates := make([]string, 4)
	borrowsNumber := make([]int, 4)
	//borrowsDates 长度为4，分别是当前日期向前-7天-14天-21天-28天
	for i := 3; i >= 0; i-- {
		var targetDateStr string = time.Now().AddDate(0, 0, -7*(i+1)).Format("2006-01-02")
		var targetDateEnd string = time.Now().AddDate(0, 0, -7*(i)).Format("2006-01-02")

		borrowsDates[i] = targetDateStr + "-" + targetDateEnd
		// 尝试将字符串转换为 time.Time 对象
		targetDateStrTime, err1 := time.Parse("2006-01-02", targetDateStr)
		if err1 != nil {
			// 例如，记录日志或返回错误
			s.logger.Info(fmt.Sprintf("Error parsing date: %v", err1))
			return
		}
		targetDateEndTime, err2 := time.Parse("2006-01-02", targetDateEnd)
		if err2 != nil {
			// 例如，记录日志或返回错误
			s.logger.Info(fmt.Sprintf("Error parsing date: %v", err2))
			return
		}
		cstLocation, err3 := time.LoadLocation("Asia/Shanghai")
		if err3 != nil {
			s.logger.Info(fmt.Sprintf("Error parsing date: %v", err3))
			return
		}
		// 或者使用FixedZone，适用于不使用夏令时的地区
		// cstLocation := time.FixedZone("CST", 8*3600)
		// 将时间转换为CST时区
		targetDateStrTimeInCST := targetDateStrTime.In(cstLocation)
		targetDateEndTimeInCST := targetDateEndTime.In(cstLocation)
		var number int

		for _, borrow := range borrowsRecord {
			if borrow.CreateTime.After(targetDateStrTimeInCST) && borrow.CreateTime.Before(targetDateEndTimeInCST) {
				// borrow.BorrowDate 在 targetDateStrTime 之后且在 targetDateEndTime 或者更早
				number++
			}
		}
		borrowsNumber[i] = number
	}
	return &v1.AdminBorrowData{
		BorrowDates:  borrowsDates,
		BorrowNumber: borrowsNumber,
	}, err
}
func (s *adminService) GetBorrowTypeData(c context.Context) ([]v1.AdminBorrowTypeData, error) {
	//1. 获取所有借阅类型
	borrowTypes, err := s.adminRepository.GetAllBookTypeList(c)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get borrowTypes when admin get borrowTypes, err: %v", err))
	}
	//2. 获取所有借阅的书籍
	borrows, err := s.adminRepository.GetBorrowsData(c)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to get borrows data when admin get borrows data, err: %v", err))
	}
	var borrowTypeData []v1.AdminBorrowTypeData
	//将所有的bookType赋值给borrowTypeData
	for _, borrowType := range borrowTypes {
		borrowTypeData = append(borrowTypeData, v1.AdminBorrowTypeData{
			BorrowType: borrowType.TypeName,
		})
	}
	//3. 根据这些借阅的书籍去book表查询其类型
	err = s.adminRepository.GetBorrowTypeData(c, &borrowTypeData, &borrows)
	if err != nil {
		if err == v1.ErrAdminBookNotFound {
			return nil, v1.ErrAdminBookNotFound
		}
		s.logger.Error(fmt.Sprintf("failed to get borrows data when admin get borrows data, err: %v", err))
	}
	return borrowTypeData, err
}
func (s *adminService) AddAdmin(ctx context.Context, v *v1.AdminRegisterRequest) error {
	err := s.adminRepository.AddAdmin(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to add admin when admin add admin, err: %v", err))
		return err
	}
	return nil
}
func (s *adminService) DeleteAllBooks(ctx context.Context, v *v1.AdminDeleteBooksRequest) error {
	err := s.adminRepository.DeleteAllBooks(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to delete all books when admin delete all books, err: %v", err))
		return err
	}
	return nil
}
func (s *adminService) PutLibraryCardRuleNumber(ctx context.Context, v *v1.AdminLibraryPutCardRuleNumberRequest) error {
	err := s.adminRepository.PutLibraryCardRuleNumber(ctx, v)
	if err != nil {
		s.logger.Error(fmt.Sprintf("failed to put libraryCard when admin put libraryCard, err: %v", err))
		if err == v1.ErrAdminLibraryCardNotFound {
			return v1.ErrAdminLibraryCardNotFound
		}

	}
	return err
}
